package com.interest.business.service.impl;

import java.util.List;

import com.interest.business.domain.vo.CouponReceiveVo;
import com.interest.business.domain.vo.InterestCodeVo;
import com.interest.business.domain.vo.UserCouponVo;
import com.interest.business.mapper.InterestRedeemCodeMapper;
import com.interest.common.exception.ServiceException;
import com.interest.common.utils.DateUtils;
import com.interest.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.interest.business.domain.InterestRedeemCode;
import com.interest.business.service.IInterestRedeemCodeService;

/**
 * 权益兑换码Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-23
 */
@Service
public class InterestRedeemCodeServiceImpl implements IInterestRedeemCodeService 
{
    @Autowired
    private InterestRedeemCodeMapper interestRedeemCodeMapper;

    /**
     * 查询权益兑换码
     * 
     * @param id 权益兑换码主键
     * @return 权益兑换码
     */
    @Override
    public InterestRedeemCode selectInterestRedeemCodeById(String id)
    {
        return interestRedeemCodeMapper.selectInterestRedeemCodeById(id);
    }

    /**
     * 查询权益兑换码列表
     * 
     * @param interestRedeemCode 权益兑换码
     * @return 权益兑换码
     */
    @Override
    public List<InterestCodeVo> selectInterestRedeemCodeList(InterestCodeVo interestRedeemCode)
    {
        return interestRedeemCodeMapper.selectInterestRedeemCodeList(interestRedeemCode);
    }

    @Override
    public List<UserCouponVo> selectUserCoupons(String phoneNumber) {
        return interestRedeemCodeMapper.selectUserCoupons(phoneNumber);
    }

    /**
     * 新增权益兑换码
     * 
     * @param interestRedeemCode 权益兑换码
     * @return 结果
     */
    @Override
    public int insertInterestRedeemCode(InterestRedeemCode interestRedeemCode)
    {
        return interestRedeemCodeMapper.insertInterestRedeemCode(interestRedeemCode);
    }

    /**
     * 修改权益兑换码
     * 
     * @param interestRedeemCode 权益兑换码
     * @return 结果
     */
    @Override
    public int updateInterestRedeemCode(InterestRedeemCode interestRedeemCode)
    {
        return interestRedeemCodeMapper.updateInterestRedeemCode(interestRedeemCode);
    }

    /**
     * 批量删除权益兑换码
     * 
     * @param ids 需要删除的权益兑换码主键
     * @return 结果
     */
    @Override
    public int deleteInterestRedeemCodeByIds(String[] ids)
    {
        return interestRedeemCodeMapper.deleteInterestRedeemCodeByIds(ids);
    }

    /**
     * 删除权益兑换码信息
     * 
     * @param id 权益兑换码主键
     * @return 结果
     */
    @Override
    public int deleteInterestRedeemCodeById(String id)
    {
        return interestRedeemCodeMapper.deleteInterestRedeemCodeById(id);
    }

    /**
     * 领取优惠券
     * @param receiveVo
     */
    @Override
    public void receiveCoupon(CouponReceiveVo receiveVo) {
        if(StringUtils.isEmpty(receiveVo.getId())){
            throw new ServiceException("参数异常");
        }
        InterestCodeVo interestCodeVo = interestRedeemCodeMapper.selectUserReceiveCoupon(receiveVo.getPhoneNumber(),receiveVo.getId());
        if(interestCodeVo==null){
            throw new ServiceException("券未分配或不存在");
        }
        if(StringUtils.equals(interestCodeVo.getSaleStatus(),"1") && interestCodeVo.getReceiveTime()==null){
            InterestRedeemCode code = new InterestRedeemCode();
            code.setId(receiveVo.getId());
            code.setSaleStatus("2");//已领取
            code.setReceiveTime(DateUtils.getNowDate());
            interestRedeemCodeMapper.updateInterestRedeemCode(code);
        }
    }
}
